<script lang="ts" setup>
import { searchStationMange,deleteStationMange } from '@/api';
import { ref,reactive, onMounted } from 'vue'
import inputtext from '@/components/inputs/inputtext.vue';
import inputdaterange from '@/components/inputs/inputdaterange.vue';
const tabledata = reactive<any[]>([]);
const currentpage = ref(1);
const pagesize = ref(100);
const total = ref(4000);
const searchref = ref<HTMLDivElement>();
const searchheight = ref(0);
onMounted(()=>{
    getData(false);
    searchheight.value = searchref.value?.clientHeight as number
    window.onresize = function(){
        searchheight.value = searchref.value?.clientHeight as number
    }
})

const selectRow:any[] = reactive([]);
const getSelectionRows = function(e:any){
    selectRow.length = 0
    selectRow.push(...e);
    console.log(selectRow)
}

const getData =async function(formquery:boolean = false){
    tabledata.length = 0
    let formdata =   objectempty(form)
    if(formquery){
        currentpage.value = 1
    }
    let {data:{data,total:totals}} =await searchStationMange(currentpage.value,pagesize.value,formdata)
    tabledata.push(...data);
    total.value = Number(totals)
}

const form = reactive<{
    wybs:string,
    start_station:string
    final_station:string
    outbound_date:any,
    inbound_date:any,
    car_no:string,
    container_no:string
}>({
    wybs:'',
    start_station:'',
    final_station:'',
    outbound_date:'',
    inbound_date:'',
    car_no:'',
    container_no:''
})

const objectempty = function(object:any){
    let newobject:any = {};
    Object.keys(object).forEach(function(key){
        if(object[key] != null && object[key] != ""){
            newobject[key] = object[key]
        }
    })
    return newobject;
}

const deleteOneRow = async function(id:string|number){
    let res = await deleteStationMange(id);
    console.log(res);
    getData()
}
</script>

<template>
    <div class="chartsshow" >
        <div ref="searchref" @keyup.enter.native="getData(true)" style="min-height: 60px;width: 100%;display: flex;align-items: center;padding: 10px 10px; box-sizing: border-box;">
            <el-form :model="form" inline size="small" >
                <inputtext style="width: 200px;" v-model="form.wybs" prop="wybs" label="唯一标识"></inputtext>
                <inputtext style="width: 200px;" v-model="form.car_no" prop="car_no" label="车号"></inputtext>
                <inputtext style="width: 200px;" v-model="form.container_no" prop="container_no" label="集装箱号"></inputtext>
                <inputtext style="width: 200px;" v-model="form.start_station" prop="start_station" label="发站"></inputtext>
                <inputtext style="width: 200px;" v-model="form.final_station" prop="final_station" label="到站"></inputtext>
                <inputdaterange style="width: 300px;" v-model="form.outbound_date" prop="outbound_date" label="出站日期"></inputdaterange>
                <inputdaterange style="width: 300px;" v-model="form.inbound_date" prop="inbound_date" label="进站日期"></inputdaterange>
                <el-form-item>
                    <el-button @click="getData(true)" >查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table :data="tabledata" size="small" stripe highlight-current-row table-layout="auto" :style="{height:`calc(100% - ${searchheight + 40}px)` }" border @selection-change="getSelectionRows">
            <el-table-column fixed type="selection" width="55" align="center"></el-table-column>
            <el-table-column type="index" label="序号" width="55" align="center"></el-table-column>
            <el-table-column label="录入站点" prop="import_station"></el-table-column>
            <el-table-column label="唯一标识" prop="wybs"></el-table-column>
            <el-table-column label="打印标识" prop="print"></el-table-column>
            <el-table-column label="单据状态" prop="status"></el-table-column>
            <el-table-column label="发站" prop="start_station"> </el-table-column>
            <el-table-column label="到站" prop="final_station"></el-table-column>
            <el-table-column label="集装箱号" prop="container_no"></el-table-column>
            <el-table-column label="集装箱箱型" prop="container_type"></el-table-column>
            <el-table-column label="集装箱箱类" prop="container_from"></el-table-column>
            <el-table-column label="集装箱标重" prop="container_weight"></el-table-column>
            <el-table-column label="集装箱状态" prop="container_status"></el-table-column>
            <el-table-column label="客户" prop="customer"></el-table-column>
            <el-table-column label="汽车号" prop="car_no"></el-table-column>
            <el-table-column label="货物" prop="goods"></el-table-column>
            <el-table-column label="数量" prop="number"></el-table-column>
            <el-table-column label="重量" prop="weight"></el-table-column>
            <el-table-column label="出站日期" prop="outbound_date"></el-table-column>
            <el-table-column label="进站日期" prop="inbound_date"></el-table-column>
            <el-table-column label="出站状态" prop="outbound_status"></el-table-column>
            <el-table-column label="进站状态" prop="inbound_status"></el-table-column>
            <el-table-column label="操作" width="130" fixed="right">
                <template #default="scope">
                    <el-button size="small" @click="deleteOneRow(scope.row.id)">删除</el-button>
                    <el-button size="small" >修改</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="display: flex;align-items: center;justify-content: end;width: 100%;height: 40px;padding-right: 20px;box-sizing: border-box;">
            <el-pagination small layout="prev, pager, next" v-model:total="total" :page-size="pagesize" v-model:current-page="currentpage" @change="getData(false)"/>
        </div>
    </div>
</template>

<style>
.chartsshow{
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>